package com.naicheng.ivote.views;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.naicheng.ivote.R;
import com.naicheng.ivote.constant.ConstantMine;
import com.naicheng.ivote.doman.BaseResponseEntity;
import com.naicheng.ivote.doman.TestListRes;
import com.naicheng.ivote.engin.BaseActivity;
import com.naicheng.ivote.parser.BaseParser;
import com.naicheng.ivote.volley.Response.Listener;

/**
 * 测试达人
 * 
 * @author guoxin
 * 
 */
public class FindTestActivity extends BaseActivity {

	ListView lv_ask;
	List<TestListRes> list = new ArrayList<TestListRes>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_findtest);
		lv_ask = (ListView) this.findViewById(R.id.lv_ask);
		getData();
		
		
		lv_ask.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mAdapter adapter = (mAdapter) lv_ask.getAdapter();
				TestListRes item = (TestListRes) adapter.getItem(position);
				
				Intent i = new Intent(FindTestActivity.this,TestDetailQuestionActivity.class);
				i.putExtra("data", item);
				startActivity(i);
			}
		});
	}

	/**
	 * 初始化请求
	 */
	private void getData() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userid", ConstantMine.userid);
		super.getDataFromServer("testlist", null, new Listener<JSONObject>() {

			@Override
			public void onResponse(JSONObject response) {
				System.out.println("response" + response.toString());
				BaseResponseEntity<TestListRes> mResponse = new BaseParser<TestListRes>()
						.parserCollection(response, TestListRes.class);
				if (mResponse != null && mResponse.getState()) {
					list = mResponse.getData();
					lv_ask.setAdapter(new mAdapter());
				}
			}
		}, this);
	}

	class mAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			viewHolder holder = null;
			if(arg1==null){
				arg1 = View.inflate(getApplicationContext(), R.layout.item_testlist, null);
				holder = new viewHolder();
				holder.tv_testlist_title = (TextView) arg1.findViewById(R.id.tv_testlist_title);
				holder.tv_testlist_content = (TextView) arg1.findViewById(R.id.tv_testlist_content);
				holder.tv_testlist_integ = (TextView) arg1.findViewById(R.id.tv_testlist_integ);
				arg1.setTag(holder);
			}else{
				holder = (viewHolder) arg1.getTag();
			}
			TestListRes data = list.get(arg0);
			if(data!=null){
				holder.tv_testlist_title.setText(data.title);
				holder.tv_testlist_content.setText(data.content);
				holder.tv_testlist_integ.setText("此题送积分："+data.integral+"分");
				if(data.answered){
					holder.tv_testlist_title.setTextColor(Color.GRAY);
					holder.tv_testlist_content.setTextColor(Color.GRAY);
				}else{
					holder.tv_testlist_title.setTextColor(Color.WHITE);
					holder.tv_testlist_content.setTextColor(Color.WHITE);
				}
			}
			return arg1;
		}

	}

	static class viewHolder {
		TextView tv_testlist_title;
		TextView tv_testlist_content;
		TextView tv_testlist_integ;
	}

}
